package com.android.job.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.android.job.fusion.FusionCode;
import com.android.job.fusion.FusionField;
import com.android.job.logic.SendResumeLogic;
import com.android.job.model.Job;

public class JobDetailActivity extends BaseActivity implements OnClickListener
{
	private TextView companyName;
	private TextView industry;
	private TextView nature;
	private TextView publish_time;
	private TextView type;
	private TextView postName;
	private TextView num;
	private TextView salary;
	private TextView contactPeople;
	private TextView contactTel;
//	private TextView job_lodging;
	private TextView degree;
	private TextView phone;
	private TextView workplace;
	private TextView jobExperience;
	private TextView jobSex;
	private TextView jobAge;
	private TextView jobTechnical;
	private TextView jobCondition;
	private TextView jobPolitics;
	private TextView jobforeignLevel;
	private TextView jobcomputer;
	private TextView jobHeight;
	private TextView jobWeight;
	private TextView jobVision;
	private TextView requirement;
	
//	private TextView housingfund;
	private TextView jobNote;
	private Button back;
	private Intent intent;
	private Job job;
	private TextView application_job;
	private ProgressDialog pd;
	private ImageView line;
	
//	private LinearLayout lodging_layout;
//	private LinearLayout housingFund_layout;
//	private LinearLayout jobExperience_layout;
//	private LinearLayout sex_layout;
//	private LinearLayout age_layout;
//	private LinearLayout technical_layout;
//	private LinearLayout workConditions_layout;
//	private LinearLayout politics_layout;
//	private LinearLayout foreignLevel_layout;
//	private LinearLayout computer_layout;
//	private LinearLayout height_layout;
//	private LinearLayout weight_layout;
//	private LinearLayout vision_layout;

	

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.job_detail);
		getIntents();
		initLayout();

	}

	private void initLayout()
	{
		companyName = (TextView) findViewById(R.id.job_companyName_job);
		industry = (TextView) findViewById(R.id.job_industry_tv);
		nature = (TextView) findViewById(R.id.nature_job_tv);
		publish_time = (TextView) findViewById(R.id.publish_time_tv);
		type = (TextView) findViewById(R.id.typeName_job_tv);
		postName = (TextView) findViewById(R.id.postName_job_tv);
		num = (TextView) findViewById(R.id.num_job_tv);
		salary = (TextView) findViewById(R.id.salary_job_tv);
		contactPeople = (TextView) findViewById(R.id.contact_tv);
		contactTel = (TextView) findViewById(R.id.contact_tel_tv);
//		job_lodging = (TextView) findViewById(R.id.job_lodging_tv);
//		degree = (TextView) findViewById(R.id.job_degree_tv);
		phone = (TextView) findViewById(R.id.moblie_tv);
		workplace = (TextView) findViewById(R.id.job_workPlace_tv);
//		jobExperience = (TextView) findViewById(R.id.other_job_experience_tv);
//		jobSex = (TextView) findViewById(R.id.other_job_sex_tv);
//		jobAge = (TextView) findViewById(R.id.other_job_age_tv);
//		jobTechnical = (TextView) findViewById(R.id.other_job_technical_tv);
//		jobCondition = (TextView) findViewById(R.id.other_job_workcondition_tv);
//		jobPolitics = (TextView) findViewById(R.id.other_job_politics_tv);
//		jobforeignLevel = (TextView) findViewById(R.id.other_job_foreignLevel_tv);
//		jobcomputer = (TextView) findViewById(R.id.other_job_computer_tv);
//		jobHeight = (TextView) findViewById(R.id.other_job_height_tv);
//		jobWeight = (TextView) findViewById(R.id.other_job_weight_tv);
//		jobVision = (TextView) findViewById(R.id.other_job_vision_tv);
//		housingfund = (TextView) findViewById(R.id.job_housingFund_tv);
		jobNote = (TextView) findViewById(R.id.jobNote_tv);
		requirement = (TextView) findViewById(R.id.jobRequire_tv);
//		lodging_layout = (LinearLayout) findViewById(R.id.other_job_lodging_layout);
//		housingFund_layout = (LinearLayout) findViewById(R.id.other_job_housfunding_layout);
//		jobExperience_layout = (LinearLayout) findViewById(R.id.other_job_experience_layout);
//		sex_layout = (LinearLayout) findViewById(R.id.other_job_sex_layout);
//		age_layout = (LinearLayout) findViewById(R.id.other_job_age_layout);
//		technical_layout = (LinearLayout) findViewById(R.id.other_job_technical_layout);
//		workConditions_layout = (LinearLayout) findViewById(R.id.other_job_workcondition_layout);
//		politics_layout = (LinearLayout) findViewById(R.id.other_job_politics_layout);
//		foreignLevel_layout = (LinearLayout) findViewById(R.id.other_job_foreignLevel_layout);
//		computer_layout = (LinearLayout) findViewById(R.id.other_job_computer_layout);
//		height_layout = (LinearLayout) findViewById(R.id.other_job_height_layout);
//		weight_layout = (LinearLayout) findViewById(R.id.other_job_weight_layout);
//		vision_layout = (LinearLayout) findViewById(R.id.other_job_vision_layout);
		back = (Button) findViewById(R.id.search_back);
		application_job = (TextView) findViewById(R.id.application_job);
    	application_job.setOnClickListener(this);
		
		back.setOnClickListener(this);
		if (job != null)
		{
			companyName.setText(job.getCompanyName());
			industry.setText(job.getIndustry());
			nature.setText(job.getNature());
			publish_time.setText(format(job.getPublicTime()));
			type.setText(job.getJobType());
			postName.setText(job.getPostName());
			num.setText(job.getNumber());
			salary.setText(job.getSalary());
			contactPeople.setText(job.getContact());
			contactTel.setText(job.getTel());
//			job_lodging.setText(job.getLodging());
//			degree.setText(job.getDegree());
			phone.setText(job.getMobile());
			workplace.setText(job.getWorkPlace());
//			housingfund.setText(job.getHousingFund());
			jobNote.setText(job.getJobNote());
			String has = getString(R.string.job_has);
			String no = getString(R.string.job_no);
			requirement.setText(job.getRequirements());
//			job.setLodging(has);
//			job.setHousingFund(has);
//			Log.d("JobDetailActivity", "requirements--->>>"+job.getRequirements());
			
//			if(has.equals(job.getLodging()))
//			{
//				lodging_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				lodging_layout.setVisibility(View.GONE);
//			}
//			
//			if(has.equals(job.getHousingFund()))
//			{
//				housingFund_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				housingFund_layout.setVisibility(View.GONE);
//			}
//			
//			String experience = job.getWorkExp();
//			
//			if(experience!=null && !"".equals(experience))
//			{
//				jobExperience.setText(experience);
//				jobExperience_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				jobExperience_layout.setVisibility(View.GONE);
//			}
//			
//			String sex = job.getSex();
//			
//			if(sex!=null && !"".equals(sex))
//			{
//				jobSex.setText(sex);
//				sex_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				sex_layout.setVisibility(View.GONE);
//			}
//			
//			String age = job.getAges();
//			
//			if(age!=null && !"".equals(age))
//			{
//				jobAge.setText(age);
//				age_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				age_layout.setVisibility(View.GONE);
//			}
//			
//			String technical = job.getTechnicalGrade();
//			
//			if(technical!=null && !"".equals(technical))
//			{
//				jobTechnical.setText(technical);
//				technical_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				technical_layout.setVisibility(View.GONE);
//			}
//			
//			String workCondition = job.getWorkConditions();
//			
//			if(workCondition!=null && !"".equals(workCondition))
//			{
//				jobCondition.setText(technical);
//				workConditions_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				workConditions_layout.setVisibility(View.GONE);
//			}
//			
//			String politics = job.getPolitics();
//			
//			if(politics!=null && !"".equals(politics))
//			{
//				jobPolitics.setText(technical);
//				politics_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				politics_layout.setVisibility(View.GONE);
//			}
//			
//			String computerLevel = job.getComputerLevel();
//			
//			if(computerLevel!=null && !"".equals(computerLevel))
//			{
//				jobcomputer.setText(computerLevel);
//				computer_layout.setVisibility(View.VISIBLE);
//			}
//			else
//			{
//				computer_layout.setVisibility(View.GONE);
//			}
			
//			if(lodging_layout.getVisibility() == View.VISIBLE &&housingFund_layout.getVisibility() == View.GONE)
//			{
//				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//				params.topMargin = 30;
//				lodging_layout.setLayoutParams(params);
//			}
//			else if(lodging_layout.getVisibility() == View.GONE &&housingFund_layout.getVisibility() == View.VISIBLE)
//			{
//				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//				params.topMargin = 30;
//				housingFund_layout.setLayoutParams(params);
//			}
			
			
		}

	}
	
	private String format(String time)
	{
		if(time.length()==8)
		{
			String year = time.substring(0, 4);
			String month = time.substring(4, 6);
			String day = time.substring(6, 8);
			String result = year+"年"+month+"月"+day+"日";
			return  result;
		}
		return "";
		
	}

	private void getIntents()
	{
		intent = getIntent();
		if (intent != null)
		{
			Bundle bundle = intent.getExtras();
			if (bundle != null)
			{
				job = (Job) bundle.getSerializable("jobdetail");
			}
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.search_back:
			finish();
			break;
		case R.id.application_job:
			if(isLogin()&&(getLoginType().equals("0"))){
				pd = new ProgressDialog(JobDetailActivity.this);
				pd.setCancelable(true);
				pd.setMessage("正在申请...");
				pd.show();
				String userId = "";
				sharedPreferences = getSharedPreferences(FusionField.POSTARR,
						Activity.MODE_PRIVATE);
				if(sharedPreferences!=null)
				{
					userId = sharedPreferences.getString("id", "");
				}
				SendResumeLogic.getInstance().sendResume(handler, userId, job.getComId(), job.getId());
			}else if(isLogin()&&(getLoginType().equals("1"))){
				Toast.makeText(JobDetailActivity.this, "企业不可以投递简历",
						Toast.LENGTH_SHORT).show();
			}else if(!isLogin()){
				Toast.makeText(JobDetailActivity.this, "请到主页登录",
						Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}
	}
	
	private Handler handler = new Handler()
	{

		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg)
		{
			pd.dismiss();
			switch (msg.what)
			{
				case FusionCode.NETWORK_ERROR:
					
					Toast.makeText(JobDetailActivity.this, R.string.network_error,
							Toast.LENGTH_SHORT).show();
					break;
				case FusionCode.NETWORK_TIMEOUT_ERROR:
					Toast.makeText(JobDetailActivity.this, R.string.network_timeout,
							Toast.LENGTH_SHORT).show();
					break;
				case FusionCode.RETURN_JSONOBJECT:
					String  success =  (String) msg.obj;
					if("1".equals(success)){
						Toast.makeText(JobDetailActivity.this, "职位申请成功",
								Toast.LENGTH_SHORT).show();
					}else if("0".equals(success)){
						Toast.makeText(JobDetailActivity.this, "职位申请失败",
								Toast.LENGTH_SHORT).show();
					}
					break;
				case FusionCode.SERVER_ERROR:
					pd.dismiss();
					String errorInif = (String) msg.obj;
					
					Toast.makeText(JobDetailActivity.this,
							errorInif, Toast.LENGTH_SHORT)
							.show();
					break;
			}

		};
	};

	
	

}
